package com.bps.ksiazkaadresowa;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;

public class MainWindow extends JFrame implements WindowListener {
	private static final long serialVersionUID = 1L;
	private JToolBar toolBar;
	private JButton addButton;
	private JButton editButton;
	private JButton removeButton;
	private JScrollPane scrollPane;
	private KAModel kaModel;
	private JTable table;
	private File dataFile;
	
	public MainWindow() {
		super();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Książka adresowa");
		setSize(500, 400);
		setLocationRelativeTo(getRootPane());
		addWindowListener(this);

		kaModel = new KAModel();
		
		toolBar = new JToolBar();
		add(toolBar, BorderLayout.PAGE_START);
		
		addButton = new JButton("Dodaj");
		addButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				OsobaDialog osobaDialog = new OsobaDialog(getRootPane());
				osobaDialog.setVisible(true);
				if (osobaDialog.result){
					kaModel.addOsoba(osobaDialog.getOsoba());
				}
			}
		});
		toolBar.add(addButton);
		
		editButton = new JButton("Edytuj");
		editButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int modyfikowanyWiersz = table.getSelectedRow();
				if (modyfikowanyWiersz > -1){
					OsobaDialog osobaDialog = new OsobaDialog(getRootPane());
					Osoba modyfikowanaOsoba = kaModel.getOsoba(modyfikowanyWiersz);
					osobaDialog.setOsoba(modyfikowanaOsoba);
					osobaDialog.setVisible(true);
					if (osobaDialog.result){
						kaModel.updateOsoba(modyfikowanyWiersz, osobaDialog.getOsoba());
					}
				}
			}
		});
		toolBar.add(editButton);
		
		removeButton = new JButton("Usuń");
		removeButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int selectedRow = table.getSelectedRow();
				if (selectedRow > -1){
					kaModel.removeOsoba(table.getSelectedRow());
				}
			}
		});
		toolBar.add(removeButton);
		
		table = new JTable(kaModel);
		
		scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);
		
		dataFile = new File(System.getProperty("user.dir") + "/kontakty.bin");
		kaModel.loadFromFile(dataFile);
	}

	@Override
	public void windowActivated(WindowEvent e) {
	}

	@Override
	public void windowClosed(WindowEvent e) {
	}

	@Override
	public void windowClosing(WindowEvent e) {
		kaModel.saveToFile(dataFile);
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
	}

	@Override
	public void windowIconified(WindowEvent e) {
	}

	@Override
	public void windowOpened(WindowEvent e) {
	}
}
